using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GGJ_DKG.Objects;
using GGJ_DKG.Menu;
using GGJ_DKG.Managers;

namespace GGJ_DKG
{

    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public static Game1 Self;

        public bool SoundOn;

        public Rectangle playerRect;
        public float playerRotation;
        public float meters;
        public bool playerDown;
        public bool fillingAir;
        public string BPMstring;
        public Vector4 airColor;
        public Vector2 airScale;

        public enum Screens
        {
            TUTORIAL, LEVEL, MENU, EXIT, CREDITS
        }
        public Screens currentScreenSelect = Screens.MENU;
        Screen currentScreen;

        

        public Game1()
        {
            Self = this;
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;
            SoundManager.AddMusic("bgMusic", Content.Load<Song>("Sound/Deep Ocean Light"));
            SoundManager.AddMusic("gameOver", Content.Load<Song>("Sound/gameOverSound"));
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            switch (currentScreenSelect)
            {
                case Screens.LEVEL:
                    currentScreen = new Level(Content.Load<Texture2D>("Scenario/BG_800_600_PB"), 
                        Vector2.Zero, 
                        Content, 
                        graphics);
                    break;
                case Screens.MENU:
                    currentScreen = new MenuScreen(Content.Load<Texture2D>("Menu/menu_principal_BG"), Vector2.Zero);
                    currentScreen.Load(Content, Window);
                    break;
                case Screens.CREDITS:
                    currentScreen = new Credits(Content.Load<Texture2D>("Menu/credits"), Vector2.Zero);
                    break;
                case Screens.TUTORIAL:
                    currentScreen = new Tutorial(Content.Load<Texture2D>("Scenario/BG_800_600_PB"), Vector2.Zero);
                    break;
                case Screens.EXIT:
                    currentScreen = new Exit(Content.Load<Texture2D>("Scenario/BG_800_600_PB"), Vector2.Zero);
                    break;
            }
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            //if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); }
                

            Input.old_ks = Input.ks;
            Input.ks = Keyboard.GetState();
            if (Input.ks.IsKeyDown(Keys.F11))
            {
                graphics.ToggleFullScreen();
            }
            switch (currentScreenSelect)
            {
                case Screens.LEVEL:
                    if (!(currentScreen is Level))
                    {
                        currentScreen = new Level(Content.Load<Texture2D>("Scenario/BG_800_600_PB"), 
                            Vector2.Zero, Content, graphics);
                        currentScreen.Load(Content, Window);
                    }
                    currentScreen.Update(gameTime);
                    
                    break;
                case Screens.MENU:
                    if (!(currentScreen is MenuScreen))
                    {
                    currentScreen = new MenuScreen(Content.Load<Texture2D>("Menu/menu_principal_BG"), Vector2.Zero);
                    currentScreen.Load(Content, Window);
                    }
                    currentScreen.Update(gameTime);
                    break;
                case Screens.CREDITS:
                    if (!(currentScreen is Credits))
                    {
                        currentScreen = new Credits(Content.Load<Texture2D>("Menu/credits"), Vector2.Zero);
                        currentScreen.Load(Content, Window);
                    }
                    currentScreen.Update(gameTime);
                    break;
                case Screens.TUTORIAL:
                    if (!(currentScreen is Tutorial))
                    {
                        currentScreen = new Tutorial(Content.Load<Texture2D>("Scenario/BG_800_600_PB"), Vector2.Zero);
                        currentScreen.Load(Content, Window);
                    }
                    currentScreen.Update(gameTime);
                    break;
                case Screens.EXIT:
                    if (!(currentScreen is Exit))
                    {
                        currentScreen = new Exit(Content.Load<Texture2D>("Scenario/BG_800_600_PB"), Vector2.Zero);
                    }
                    currentScreen.Update(gameTime);
                    break;
            }
            
            
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            // TODO: Add your drawing code here
            spriteBatch.Begin();
            currentScreen.Draw(spriteBatch, gameTime);
            spriteBatch.End();
            base.Draw(gameTime);
        }

    }
}